---@class components
---@field bh_mobius_lv component_bh_mobius_lv

local function on_val(self, value)
    self.inst.replica.bh_mobius_lv:SetVal(value)
end

local function on_exp(self, value)
    self.inst.replica.bh_mobius_lv:SetEXP(value)
end

---@class component_bh_mobius_lv
---@field inst ent
---@field val integer # 等级
---@field exp number # 经验值
---@field max_exp number # 经验值最大
local bh_mobius_lv = Class(
---@param self component_bh_mobius_lv
---@param inst ent
function(self, inst)
    self.inst = inst
    self.val = 0
    self.exp = 0
    self.max_exp = 0

    inst:ListenForEvent('onhitother',
    ---comment
    ---@param inst ent
    ---@param data event_data_onhitother
    function (inst, data)
        if data then
            local victim = data.target
            if victim and victim.components.combat and not victim:HasTag('structure') and not victim:HasTag('companion') and not S_BH_MOBIUS:checkAlive(victim) then
                local maxhp = victim.components.health.maxhealth
                -- 获取经验值
                self:GetEXP(maxhp)


            end
        end
    end)
end,
nil,
{
    val = on_val,
    exp = on_exp,
})

function bh_mobius_lv:OnSave()
    return {
        val = self.val,
        exp = self.exp
    }
end

function bh_mobius_lv:OnLoad(data)
    self.val = data.val or 0
    self.exp = data.exp or 0
end

function bh_mobius_lv:GetEXP(delta)
    self.max_exp = self:CalcEXPMAX()
    self.exp = self.exp + delta
    while self.exp > 0 do
        if self.exp >= self.max_exp then
            self.exp = self.exp - self.max_exp
            self.val = self.val + 1
            self.max_exp = self:CalcEXPMAX()
        else
            break
        end
    end
end

function bh_mobius_lv:CalcEXPMAX()
    return math.floor(math.log(self.val+1)*2000+1000)
end

return bh_mobius_lv